//
// Created by Tong on 2020/10/25.
//

#include <iostream>

#include "GRDECLGrid.h"
#include "MeshInterpolation.h"
#include "MeshInterpolation.h"

int main(int argc, char *argv[])
{
    if(argc<=2)
    {
        std::cout<<"At least two arguments are required!"<<std::endl;
        return 1;
    }

    if(argc>4)
    {   
        if(std::string(argv[3]) == "perf_test")
        {
            MeshInterpolation::perfTestMode_ = true;
        }
        MeshInterpolation::threadNum_ = std::stoi(argv[4]);
    }

    // 输入文件1文件名
    std::string inputFile1(argv[1]);
    std::cout<<"input file1:"<<inputFile1<<std::endl;
    // 输入文件1构造点云网格
    PointFieldMesh inputData(inputFile1);
    // 输出输入文件1的vtk可视化文件
    inputData.vtkCellNodeDataWriter("inputData.vtk",inputData.data_);

    // 输入文件2文件名
    std::string inputFile2(argv[2]);
    std::cout<<"input file2:"<<inputFile2<<std::endl;
    // 输入文件2构造点云网格
    PointFieldMesh outputField(inputFile2);

    // 构造插值器
    MeshInterpolation interpolator(inputData.points_,outputField.points_);

    // 根据输入文件插值点数目初始化数据
    outputField.data_.resize(outputField.points_.size());
    
    // 优化内存 清除赛题2中不必要数据
    interpolator.freeTree();
    system("pause for memory check");

    // 网格1数据插值到网格2
    interpolator.interpolateMesh1toMesh2(inputData.data_,outputField.data_);
    // 输出插值后的数据
    outputField.vtkCellNodeDataWriter("outputField.vtk",outputField.data_);
    // *****输出赛题要求的插值后数据********
    outputField.writeRequiredData("S1Data.txt");

    // 反向插值数据
    DoubleList reverseIpData(inputData.data_.size());
    // 网格2数据反向插值到网格1
    interpolator.interpolateMesh2toMesh1(outputField.data_,reverseIpData);
    // 输出反向插值数据
    inputData.vtkCellNodeDataWriter("reverseIpData.vtk",reverseIpData);
    // 计算误差
    DoubleList error = calcError(inputData.data_,reverseIpData);
    // 输出误差
    inputData.vtkCellNodeDataWriter("error.vtk",error);

    return 0;
}
